"""

@Date    ：2025-08-15 15:25
"""
from django.forms import CharField, IntegerField, ValidationError, GenericIPAddressField
from django.contrib.auth.hashers import make_password, check_password

from utils.form import Form
from app.models import CityModel, MachineRoomModel, HostModel


class CityForm(Form):
    name = CharField(label='名称', max_length=16, required=True, error_messages={
        'required': '请输入城市名称！'
    })


class MachineRoomForm(Form):
    city = CharField(label='城市', required=True, error_messages={
        'required': '请输入机房所在城市！'
    }, max_length=16)

    def clean_city(self):
        city_model, is_create = CityModel.objects.get_or_create(name=self.data['city'])
        return city_model.id


class HostForm(Form):
    machine_room_id = IntegerField(label='机房id', required=True, error_messages={
        'required': '请选择机房！'
    })
    password = CharField(label='root密码', required=True, error_messages={
        'required': '请输入root密码！'
    })
    ip = GenericIPAddressField(label='IP', required=True, error_messages={
        'required': '请输入IP！'
    })
    cpu = IntegerField(label='CPU', required=True, error_messages={
        'required': '请输入CPU！'
    })
    memory = IntegerField(label='内存', required=True, error_messages={
        'required': '请输入内存！'
    })
    motherboard = CharField(label='主板', required=True, error_messages={
        'required': '请选择主板！'
    })
    hard_disk = IntegerField(label='硬盘大小', required=True, error_messages={
        'required': '请输入硬盘大小！'
    })
    is_use = IntegerField(label='状态', required=True, error_messages={
        'required': '请输入主机状态！'
    })

    def clean_machine_room_id(self):
        machine_room_id = self.cleaned_data['machine_room_id']
        try:
            MachineRoomModel.objects.get(id=machine_room_id)
            return machine_room_id
        except MachineRoomModel.DoesNotExist:
            raise ValidationError('机房不存在！')

    def clean_password(self):
        password = self.data['password']
        password1 = self.data['password1']

        if len(password1) < 6:
            raise ValidationError('密码长度不能小于6！')
        if len(password1) > 8:
            raise ValidationError('密码长度不能大于8！')
        if password != password1:
            raise ValidationError('两次输入密码不正确！')

        return make_password(password)

    def clean_motherboard(self):
        motherboard = self.data['motherboard']
        for item in HostModel.motherboard_choices:
            if item[1] == motherboard:
                return item[0]
        raise ValidationError('请选择一个合适的主板！')

    def clean_is_use(self):
        is_use = self.data['is_use']
        for item in HostModel.state_choices:
            if item[0] == is_use:
                return item[0]
        raise ValidationError('状态不存在！')


class PutHostForm(HostForm):
    old_password = CharField(label='root密码', required=True, error_messages={
        'required': '请输入旧的root密码！'
    })

    def clean_password(self):
        password = self.data['password']
        password1 = self.data['password1']
        old_password = self.data['old_password']
        nid = self.data.pop('id', None)

        if len(password1) < 6:
            raise ValidationError('密码长度不能小于6！')
        if len(password1) > 8:
            raise ValidationError('密码长度不能大于8！')
        if password != password1:
            raise ValidationError('两次输入密码不正确！')

        try:
            host_model = HostModel.objects.get(id=nid)
        except HostModel.DoesNotExist:
            raise ValidationError('主机数据不存在！')
        if not check_password(old_password, host_model.password):
            raise ValidationError('旧密码输入错误！')
        if check_password(password, host_model.password):
            raise ValidationError('新密码不能与旧密码相同')

        return make_password(password)
